package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.bean.MyBean;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.BuyOneModel;
import cn.edu.neusoft.wuguangsheng.market.model.MyModel;

public class BuyActivity extends BaseActivity {

    Spinner spinner;
    ArrayAdapter<String> arrayAdapter;
    String[] str = { "7:00-8:00", "8:00-9:00","9:00-10:00","10:00-11:00","11:00-12:00",
            "12:00-13:00","13:00-14:00","14:00-15:00","15:00-16:00","16:00-17:00","17:00-18:00",
            "18:00-19:00","19:00-20:00","20:00-21:00","21:00-22:00"};
    ImageView img_buy_ret;
    EditText buy_time;
    Button buy_buy;
    String uid,fid,num,suggesttime,uname;
    double sum1;
    String mrid;
    Boolean up=false;
    String phone,shname,address;
    String shsj;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_buy);
        img_buy_ret=(ImageView)findViewById(R.id.img_buy_ret);

        //buy_time=(EditText)findViewById(R.id.et_buy_time);
        buy_buy=(Button)findViewById(R.id.btn_buy_buy);
        fid = getIntent().getStringExtra("fid");
        num = getIntent().getStringExtra("num");
        mrid = getIntent().getStringExtra("mrid");
        sum1 = getIntent().getDoubleExtra("sum",0.00);
        spinner = (Spinner) findViewById(R.id.spi1);
        arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_spinner_item, str);
        // spinner.setPrompt("测试");
        spinner.setPromptId(R.id.spi);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new ItemClick());
    }
    private class ItemClick implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
                                   long arg3) {
            shsj=(String) parent.getItemAtPosition(arg2);
//            Toast.makeText(CartBuyActivity.this,
//                    "点击的是" + parent.getItemAtPosition(arg2), Toast.LENGTH_SHORT)
//                    .show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    }
    @Override
    void initEvent() {
    buy_buy.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        uid=getid();
            BuyOneModel buyOneModel=new BuyOneModel();
            buyOneModel.buyOneFood(uid,fid,num,sum1,shsj,mrid,tListener);

    }
});
        img_buy_ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    void initDate() {

    }
TListener<RegisterBean> tListener=new TListener<RegisterBean>() {
    @Override
    public void onResponse(RegisterBean registerBean) {
        String success=registerBean.getSuccess();

        if(success.equals("1")){
            Toast.makeText(BuyActivity.this,"下单成功，请在你预计送达的时间里等待你购买的东西",Toast.LENGTH_LONG).show();
            finish();
        }
        else if(success.equals("2")){
            Toast.makeText(BuyActivity.this,"收货人为空，请填写收货人信息",Toast.LENGTH_SHORT).show();
            uid=getid();
            MyModel myModel=new MyModel();
            myModel.getMyinfo(uid,mListener);



        }
        else{
            Toast.makeText(BuyActivity.this,"系统错误",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(BuyActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
    }
};
    TListener<MyBean> mListener=new TListener<MyBean>() {
        @Override
        public void onResponse(MyBean myBean) {
            String uname=myBean.getUname();
            phone=myBean.getPhone();
            String pic=myBean.getPic();
            shname=myBean.getShname();
            address=myBean.getAddress();
           // Toast.makeText(BuyActivity.this,address,Toast.LENGTH_SHORT).show();
            Intent intentq=new Intent(BuyActivity.this,MySetActivity.class);
            intentq.putExtra("shname",shname);
            intentq.putExtra("phone",phone);
            intentq.putExtra("address",address);
            startActivity(intentq);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(BuyActivity.this,"???", Toast.LENGTH_SHORT).show();
        }
    };


}
